/*
 * VHAssociationSelector.java
 * Created on 10 sept. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.widget;

import java.util.List;

import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.rpc.VHRpcResultReceiver;
import com.hockeo.vhweb.client.ui.base.VHSelector;
import com.hockeo.vhweb.client.utils.VHJsData;
import com.hockeo.vhweb.client.utils.VHJsSimpleData;

/**
 * Combo box that displays all national associations. It also allows to choose an "ALL"
 * option to select all associations at once.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHAssociationSelector extends VHSelector implements VHRpcResultReceiver<List<VHJsData>>
{
  public static final String ID_ALL_ASSOCS = "ALL";

  public VHAssociationSelector()
  {
    getCombobox().setDisplayField( "id_translated" );
    getCombobox().setValueField( "id" );
    getCombobox().setWidth( 200 );
    getCombobox().setTemplate( getFlagTemplate() );
  }

  /**
   * Sets the provided associations to the internal store.
   */
  public void setRpcData( List<VHJsData> listAssocs, String strPurpose )
  {
    VHJsSimpleData assocAll = new VHJsSimpleData( ID_ALL_ASSOCS, VHMain.getConstants().showAllAssocs() );

    assocAll.set( "id_translated", assocAll.getLabel() );

    getStore().removeAll();
    getStore().add( assocAll );
    getStore().add( listAssocs );

    // By default, no particular association is selected
    //
    getCombobox().setValue( assocAll );

    setInitialised( true );
  }

  private native String getFlagTemplate()
  /*-{
    return  [
    '<tpl for=".">',
    '<div class="x-combo-list-item" style="padding-left: 22px; background-image:url(images/flags/16x16/{[values.id]}.png); background-repeat: no-repeat; background-position: 2px 2px;">{[values.id_translated]}</div>',
    '</tpl>'
    ].join("");
  }-*/;
}
